package org.example.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.example.entity.vo.response.IndexFormVO;
import org.example.entity.vo.response.PostWIthImageVO;

import java.util.List;
import java.util.Map;

@Mapper
public interface CustomizeMapper {
    @Select("""
            SELECT a.id,a.title,a.text,a.time,c.path
            FROM db_post AS a
            INNER JOIN rs_image_post AS b ON b.pid=a.id
            INNER JOIN db_image AS c ON c.id=b.iid
            WHERE a.id = #{postID}""")
    PostWIthImageVO getPostWithImageByPostID(@Param("postID") Integer postID);

    @Select("""
            SELECT a.id AS post_id,a.title AS post_title,f.reply AS post_reply,a.like AS post_like,FLOOR(RAND()*10) AS post_share,e.id AS forum_id,e.name AS forum_name,e.icon AS forum_icon,e.type AS forum_type,c.path AS image_path
            FROM db_post AS a
            INNER JOIN rs_image_post AS b ON b.pid=a.id
            INNER JOIN db_image AS c ON c.id=b.iid
            INNER JOIN (SELECT MAX(a.pid) AS pid,a.fid AS fid
            FROM (SELECT a.id AS pid,c.id AS fid
            FROM db_post AS a
            INNER JOIN rs_forum_post AS b ON a.id=b.pid
            INNER JOIN db_forum AS c ON c.id=b.fid
            ORDER BY RAND()) AS a
            GROUP BY a.fid) AS d ON d.pid=a.id
            INNER JOIN db_forum AS e ON e.id=d.fid
            INNER JOIN (SELECT pid,COUNT(mid) AS reply
            FROM rs_post_message
            GROUP BY pid) AS f ON f.pid=a.id""")
    List<IndexFormVO> getIndexForm();

    @Select("""
            SELECT uid
            FROM rs_account_forum
            WHERE fid=#{forumID} AND type = 'release'""")
    Integer getForumLeaderIDByForumID(@Param("forumID") Integer forumID);

    @Select("""
            SELECT *\s
            FROM db_account_extend
            WHERE uid=#{userID}""")
    Map<String, Object> getAccountExtendByUserID(@Param("userID") Integer userID);

}
